package us.nchu.tomtiger.interceptor;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import us.nchu.tomtiger.utils.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by XiongXuan on 2018/1/7.
 * 拦截所有的网页
 */
@Component
public class AllInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        return true;
    }

    /**
     * 拦截处理后的请求
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     */
    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {
        if (modelAndView != null) {
            // 如果用户为登录状态，设置header.jsp参数
            if (UserUtil.isLogin(httpServletRequest)) {
                modelAndView.addObject("isLogin", 1);
                modelAndView.addObject("loginNickName", httpServletRequest.getSession().getAttribute("nickName"));
            }
        }
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
